namespace DotNetNuke.Security.Authentication
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Security;
    using DotNetNuke.Security.Roles;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web;

    public class UserController
    {
        private string mProviderTypeName = "";

        public UserController()
        {
            Configuration _config = Configuration.GetConfig();
            this.mProviderTypeName = _config.ProviderTypeName;
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created 
        /// </history>
        /// -------------------------------------------------------------------
        public int AddDNNUser(DotNetNuke.Security.Authentication.UserInfo AuthenticationUser)
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            PortalSecurity objSecurity = new PortalSecurity();
            DotNetNuke.Entities.Users.UserController objDNNUsers = new DotNetNuke.Entities.Users.UserController();
            DotNetNuke.Security.Authentication.UserController objAuthUsers = new DotNetNuke.Security.Authentication.UserController();
            DotNetNuke.Entities.Users.UserInfo objDNNUser = AuthenticationUser;
            int AffiliateId = -1;
            if (HttpContext.Current.Request.Cookies["AffiliateId"] != null)
            {
                AffiliateId = int.Parse(HttpContext.Current.Request.Cookies["AffiliateId"].Value);
            }
            int UserID = -1;
            UserID = objDNNUsers.AddUser(objDNNUser);
            if (((AuthenticationUser.AuthenticationExists && (UserID > -1)) ? 1 : 0) != 0)
            {
                AuthenticationUser.UserID = UserID;
                AddUserRoles(_portalSettings.PortalId, AuthenticationUser);
            }
            return UserID;
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// This routine is more accurated, 
        /// Prevent user assign to admin role in case user logon as LOCAL\Administrator
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static void AddUserRoles(int PortalID, DotNetNuke.Security.Authentication.UserInfo AuthenticationUser)
        {
            GroupController objGroupController = new GroupController();
            ArrayList colGroup = objGroupController.GetGroups();
            RoleController objRoles = new RoleController();
            try
            {
                IEnumerator refObjectHelperL0=null;
                try
                {
                    refObjectHelperL0 = colGroup.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        GroupInfo authenticationGroup = (GroupInfo) refObjectHelperL0.Current;
                        if (objGroupController.IsAuthenticationMember(authenticationGroup, AuthenticationUser))
                        {
                            objRoles.AddUserRole(PortalID, AuthenticationUser.UserID, authenticationGroup.RoleID, Null.NullDate);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// User object with info obtained from Active Directory
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public DotNetNuke.Security.Authentication.UserInfo GetUser(string LoggedOnUserName)
        {
            return AuthenticationProvider.Instance(this.mProviderTypeName).GetUser(LoggedOnUserName);
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// User object with info obtained from Active Directory
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public DotNetNuke.Security.Authentication.UserInfo GetUser(string LoggedOnUserName, string LoggedOnPassword)
        {
            return AuthenticationProvider.Instance(this.mProviderTypeName).GetUser(LoggedOnUserName, LoggedOnPassword);
        }
    }
}

